package createType.AbstractFactory.example.maze;

import static org.junit.Assert.*;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.junit.Before;
import org.junit.Test;

public class TestMaze {
	
	public Maze m;

	@Test
	public void test() {
		fail("Not yet implemented");
	}
	
	@Before
	public void testMaze_create(){
		
		Room r101 = new Room(101);
		Room r102 = new Room(102);
		Room r103 = new Room(103);
		MapSite w = new Wall(); 
		MapSite d101_102 = new Door(r101,r102);
		MapSite d102_101 = new Door(r102,r101);
		MapSite d102_103 = new Door(r102,r103);
		MapSite d103_102 = new Door(r103,r102);
		MapSite[] m101 = {w,w,d101_102,w};
		MapSite[] m102 ={w,w,d102_103,d102_101};
		MapSite[] m103 ={w,w,w,d103_102};
		r101.setDirection(m101);
		r102.setDirection(m102);
		r103.setDirection(m103);
		m = new Maze(r101,r103);
		m.rooms.add(r101);
		m.rooms.add(r102);
		m.rooms.add(r103);
		m.printMaze();
	}
	
	@Test
	public void testOperate(){
		
	}
	@Test
	public void testAction_1(){
		List<Room> pathlog = new ArrayList<Room>();
		pathlog.add(m.getStart());
		try{
			while(true){
				/*show your current position*/
				Room current =pathlog.get(pathlog.size()-1);
				System.out.println(" your current position:"+current.RoomNo);
				if(current.RoomNo==m.getExit()[0].RoomNo){
					break;
				}
				InputStream is = System.in;
				Scanner scan = new Scanner(is);
				System.out .print("enter your direction:");
				int id = scan.nextInt(); // scan.nextInt();
				Direction inputD = checkInput(id);
				if(inputD == null){
					System.out.println("error,please enter again");
					continue;
				}
				Room next = nextRoom(current.getSide(inputD));
				if(next == null){
					System.out.println("NO. please another choice.");
					pathlog.add(current);
					continue;
				}
				pathlog.add(next);
				
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		System.out.println("Congratulation!");
		for(Room r:pathlog){
			System.out.print("--"+r.RoomNo);
		}
	}
	public Direction checkInput(int id){
		Direction inputD;
		switch(id){//java 1.7以下不支持String类型
		case 1:
			inputD = Direction.South;
			break;
		case 2:
			inputD = Direction.North;
			break;
		case 3:
			inputD = Direction.East;
			break;
		case 4:
			inputD = Direction.West;
			break;
		default:
			inputD = null;
		}
		return inputD;
	}
	
	public Room nextRoom(MapSite m){
		if(m.getClass().getSimpleName().equals("Door")){
			return (Room) ((Door)m).getPath().getNodes().get(1);
		}
		return null;
	}
	

}
